/**
 * 
 */
package com.wolfpire.info.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.info.dao.ProfessionalDetailCountInfoDao;
import com.wolfpire.info.model.ProfessionalDetailCountInfo;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;

/**
 * @author lihd
 *
 */

@Repository("professionalDetailCountInfoDao")
public class ProfessionalDetailCountInfoDaoImpl extends
		BaseHibernateDao<ProfessionalDetailCountInfo, Long> implements
		ProfessionalDetailCountInfoDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProfessionalDetailCountInfo> list(
			ProfessionalDetailCountInfo professionalDetailCountInfo) {
		Criterion[] criterions = this.createFilter(professionalDetailCountInfo);
		Criteria c = this.createCriteria(criterions);
		c.addOrder(Order.asc("seq"));
		return c.list();
	}

	@Override
	public void deleteByProfessionalCountInfoId(Long professionalCountInfoId) {
		StringBuffer executeSql = new StringBuffer();
		executeSql.append(" delete from t_professional_detail_count_info where professionalcountinfo_id = ?");
		Object[] values = new Object[]{professionalCountInfoId};
		this.executeSql(executeSql.toString(), values);
	}

	@Override
	public void batchSave(
			List<ProfessionalDetailCountInfo> professionalDetailCountInfos, Long professionalCountInfoId) {
		// TODO Auto-generated method stub
		if (CollectionUtils.isNotEmpty(professionalDetailCountInfos)) {
			Session session = this.getSession();
			int size = professionalDetailCountInfos.size();
			for (int i = 0; i < size; i++) {
				ProfessionalDetailCountInfo info = professionalDetailCountInfos.get(i);
				info.setProfessionalCountInfoId(professionalCountInfoId);
				if (null != info.getSeq()) {
					info.setSeq(i);
				}
				session.save(info);
				if ((i + 1) % 50 == 0) {
					session.flush();
					session.clear();
				}
			}
			/*for (ProfessionalDetailCountInfo info : professionalDetailCountInfos) {
				session.save(info);
			}
			session.flush();
			session.clear();*/
		}
	}
	
	private Criterion[] createFilter(ProfessionalDetailCountInfo filterProfessionalDetailCountInfo) {
		if (null == filterProfessionalDetailCountInfo) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterProfessionalDetailCountInfo.getId()) {
			criterions.add(Restrictions.eq("id", filterProfessionalDetailCountInfo.getId()));
		}
		if (null != filterProfessionalDetailCountInfo.getProfessionalCountInfoId()) {
			criterions.add(Restrictions.eq("professionalCountInfoId", filterProfessionalDetailCountInfo.getProfessionalCountInfoId()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

}
